package com.hywood.exception;

import com.hywood.constant.Constant;
import com.hywood.util.ResultUtil;
import com.hywood.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author lantl
 */
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
    private static final Logger errorlogger=LoggerFactory.getLogger(Constant.ERROR_LOGGER);
    private static final Logger businesslogger=LoggerFactory.getLogger(Constant.BUSINESS_LOGGER);
    //声明要捕获的异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public <T> Result<?> defaultExcepitonHandler(Exception e) {
        if(e instanceof BusinessException) {
            BusinessException businessException = (BusinessException)e;
            businesslogger.error(e.getMessage(),this.getClass());
            return ResultUtil.error(businessException.getCode(), businessException.getMessage());
        }
        //未知错误
        errorlogger.error(e.getMessage(),this.getClass());
        return ResultUtil.error(-1, e.toString());
    }

}
